/**
 * 上海中赢金融信息服务有限公司
 * Copyright (c) 2017-2027 Chinazyjr,Inc.All Rights Reserved.
 */

package com.sys.midware.hadoop.hdfs.filter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.PathFilter;

import com.sys.midware.logger.Logger;
import com.sys.midware.logger.LoggerFactory;

/**
 * 文件
 * <b>Date:</b>     2016年3月30日 上午10:30:29<br/> 
 * @author   mobing
 * @version  	 
 */
public class FilePathFilter implements PathFilter {
    private static Logger logger = LoggerFactory.getLogger(FilePathFilter.class);
    //需要读取文件名必须包含fileName字符串  
    private String fileName;  
      
    public FilePathFilter(String fileName){  
        this.fileName = fileName;  
    }  

    @Override
    public boolean accept(Path path) {
//        logger.info("begin to filte,path=" + path);
        boolean res = false;  
        try {
            if(path == null){
                res = false;
            }
            Pattern pattern = Pattern.compile(fileName);
            Matcher matcher = pattern.matcher(path.toString());
            if(matcher.find() || path.toString().indexOf(fileName)!=-1){
                res=true;
            }
        } catch (Exception e) {
            logger.error("FilePathFilter error![path="+path +",fileName=" +fileName +"]" ,e);
        }  
        return res;
    }

}

